<?php

if( isset( $_POST['hostname'] ) 
&& isset( $_POST['username'] ) && isset( $_POST['password'] ) && isset( $_POST['db'] ) 
&& isset( $_POST['adminusername'] ) && isset( $_POST['adminpassword'] ) && isset( $_POST['adminemail'] ))
{
	file_put_contents( Globals::Get( 'AppPath') . '/Config.include', '<?php
	
	DBConnection::$Hostname = \'' . $_POST['hostname'] . '\';
	DBConnection::$Username = \'' . $_POST['username'] . '\';
	DBConnection::$Password = \'' . $_POST['password'] . '\';
	
	AccountManager::$Database = \'' . $_POST['db'] . '\';
	ContentManager::$Database = \'' . $_POST['db'] . '\';
	
	$IsRunning=true;
	?>' );
	
	// include configuration
	require_once Globals::Get( 'AppPath') . '/Config.include';

	// setup database
	require_once Globals::Get( 'AppPath') . '/Modules/Setup.include';

	// create admin user
	$LoginName = $_POST['adminusername'];
	$Password = $_POST['adminpassword'];
	$Email = $_POST['adminemail'];
	$Result = AccountManager::GetInstance( )->CreateAccount( $LoginName, 'Root Administrator', $Email, $Password );
	AccountManager::GetInstance( )->SetUserLevel( $LoginName, -1 );
	
	Session::GetInstance( )->LoginName = $LoginName;
	?>
	<h1>Installation finished!</h1>
	<?php
}
else
{
	?>
	<form method="post">
	<h3>MySQL</h3>
		sqlhostname<input type="text" name="hostname" /><br />
		username<input type="text" name="username" /><br />
		password<input type="text" name="password" /><br />
		database<input type="text" name="db" /><br />
		<h3>Settings</h3>
		admin username<input type="text" name="adminusername" /><br />
		admin password<input type="password" name="adminpassword" /><br />		
		admin email<input type="text" name="adminemail" /><br />		
		<input type="submit" />
	</form>
	<?php
}

?>